﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using ELearningSystem.UI.LearnerServiceReference;

namespace ELearningSystem.UI.Account
{
    public partial class Registration : System.Web.UI.Page
    {
        LearnerServiceSoapClient userManager = new LearnerServiceSoapClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                messageLabel.Text = "";
                messageLabel.ForeColor = Color.Green;
                messageLabel.Text = Request.QueryString["SignUpMessage"];
            }

        }

        protected void registerButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (passwordTextBox.Text.Equals(retypePasswordTextBox.Text))
                {
                    if (nameTextBox.Text != "" && emailTextBox.Text != "" && passwordTextBox.Text != "")
                    {
                        LearnerDAO learner = new LearnerDAO();
                        learner.Name = nameTextBox.Text;
                        learner.EmailAddress = emailTextBox.Text;
                        learner.Password = passwordTextBox.Text;
                        string saveMessage;

                        if (AuthenticatedLearner(learner)&userManager.SaveOrUpdateLearner(learner,out saveMessage))
                        {
                           FormsAuthentication.RedirectFromLoginPage(learner.Name, true);
                        }
                        else
                        {
                            messageLabel.ForeColor = Color.Red;
                            messageLabel.Text = saveMessage;
                        }
                        
                       
                    }
                    else
                    {
                        messageLabel.ForeColor = Color.Red;
                        messageLabel.Text = "Please fill-up all field properly";
                    }
                    
                }
                else
                {
                    messageLabel.ForeColor = Color.Red;
                    messageLabel.Text = "Password is not confirmed!";
                }
                
                

            }
            catch (Exception exception)
            {

                messageLabel.ForeColor = Color.Red;
                messageLabel.Text = exception.Message;
            }
        }
        public bool AuthenticatedLearner(LearnerDAO learner)
        {
            try
            {
                MembershipCreateStatus staus;
                MembershipUser aMUser = Membership.CreateUser(learner.Name, learner.Password, learner.EmailAddress);
                string[] users = new string[1];
                users[0] = aMUser.UserName;
                Roles.AddUsersToRole(users, "Learner");
                return true;
            }
            catch (Exception exception)
            {
                throw new Exception("User can't be created!", exception);
            }
        }
    }
}